from rest_framework.generics import ListAPIView, ListCreateAPIView

from apps.meiduo_admin.paginations import MyPage
from apps.meiduo_admin.serializers.user_serializers import UserModelSerializer
from apps.users.models import User


class UserView(ListCreateAPIView):
    queryset = User.objects.filter(is_staff=True).order_by('id')
    serializer_class = UserModelSerializer
    # 指定分页器
    pagination_class = MyPage

    def get_queryset(self):
        keyword = self.request.query_params.get("keyword")

        if keyword:
            # 返回过滤后的数据---名字包含这个keyword的
            return self.queryset.filter(username__contains=keyword)
        else:
            return self.queryset.all()


